﻿// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using System.Windows.Forms;
using Microsoft.WindowsCE.Forms;
using svs.Mobile.BeyondMedia.FeedCore;
using svs.Mobile.BeyondMedia.Objects;
using svs.Mobile.BeyondMedia.Properties;
using svs.Mobile.BeyondMedia.UiCore;

namespace svs.Mobile.BeyondMedia.Forms.Settings
{
	public partial class PlayerSettings : UserControl, ISettingsPage
	{
		public PlayerSettings()
		{
			InitializeComponent();
		}

		public static string PageName
		{
			get { return "Player Settings"; }
		}

		public static string PageDescription
		{
			get
			{
				return "Set Default Player, File types... ";
			}
		}

		public static SettingsPageHolderForm.SettingsPages PageID
		{
			get { return SettingsPageHolderForm.SettingsPages.Player; }
		}

		#region ISettingsPage Members

		public bool CheckAndSaveSettings()
		{
			try
			{
				Configuration.ShortSkipInterval = int.Parse(ShortSkip.Text);
			}
			catch
			{
				MessageBoxEx.Show(Resources.ShortSkipValidationMsg, Resources.ShortSkipValidationTitle, MessageBoxButtons.OK,
				                  MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
				return false;
			}

			try
			{
				Configuration.LongSkipInterval = int.Parse(LongSkip.Text);
			}
			catch
			{
				MessageBoxEx.Show(Resources.LongSkipValidationMsg, Resources.LongSkipValidationTitle, MessageBoxButtons.OK,
				                  MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
				return false;
			}


			Configuration.MediaEndAction = (CoreHelper.MediaEndAction) MediaEndAction.SelectedIndex;

			Configuration.PlayerIntegration = (PlayerIntegration) PlayerSelector.SelectedIndex;

			SkinManagerBase mgr = (SkinManagerBase) (ViewManager.MediaPlayer.PlayerSkinManager);

			if (ViewManager.MediaPlayer.PlayerSkinManager.CurrentPlayerSkin != PlayerSkin.SelectedItem as ISkin)
			{
				if (!mgr.MatchesDeviceScreenOrientation(PlayerSkin.SelectedItem as ISkin))
				{
					if (
						MessageBoxEx.Show(Resources.WrongSkinOrientation, "", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation,
						                  MessageBoxDefaultButton.Button1) == DialogResult.No)
						return false;
				}

				ViewManager.MediaPlayer.PlayerSkinManager.CurrentPlayerSkin = PlayerSkin.SelectedItem as ISkin;
			}

			Configuration.InternalPlayerSupportedFileExtensions = SupportedExtensions.Text;

			Configuration.AutoScreenOffOnPlayback = AutoScreenOff.Checked;

			Track.ReloadSupportedFileTypes();

			return true;
		}

		public void LoadPageSettings()
		{
			ShortSkip.Text = Configuration.ShortSkipInterval.ToString();
			LongSkip.Text = Configuration.LongSkipInterval.ToString();

			if (PlatformDetection.IsSmartphone())
			{
				InputModeEditor.SetInputMode(ShortSkip, InputMode.Numeric);
				InputModeEditor.SetInputMode(LongSkip, InputMode.Numeric);
			}

			MediaEndAction.SelectedIndex = (int) Configuration.MediaEndAction;
			PlayerSelector.SelectedIndex = (int) Configuration.PlayerIntegration;
			PlayerSkin.DataSource = ViewManager.MediaPlayer.PlayerSkinManager.InstalledSkins;
			PlayerSkin.SelectedItem = ViewManager.MediaPlayer.PlayerSkinManager.CurrentPlayerSkin;

			SupportedExtensions.Text = Configuration.InternalPlayerSupportedFileExtensions;

			AutoScreenOff.Checked = Configuration.AutoScreenOffOnPlayback;
		}

		public bool ShouldReloadRepositoryOnDone
		{
			get { return false; }
		}

		#endregion

		private void JumpToKeymaps_Click(object sender, EventArgs e)
		{
			CommandManager.CmdShowShortcutEditorForm();
		}
	}
}